layui.use(['laydate', 'laypage', 'layer', 'table', 'upload', 'element','form'], function(){
	  var laydate = layui.laydate //日期
	  ,laypage = layui.laypage //分页
	  layer = layui.layer //弹层
	  ,table = layui.table //表格
	  ,upload = layui.upload //上传
	  ,element = layui.element //元素操作
	  ,form = layui.form; //form表单
	 
	  
	  //监听Tab切换
	  element.on('tab(demo)', function(data){
	    layer.msg('切换了：'+ this.innerHTML);
	    console.log(data);
	  });
	  
	  //执行一个 table 实例
	  table.render({
	     elem: '#DrugRepertory'
	    ,width:674
	    ,height: 406
	    ,url: getRootPath()+'Repertory/findAllDrugOrMaterials.action?filId=1&condition=drug' //数据接口
	    ,page: true //开启分页
	    ,method:'post'
	    ,size: 'sm'
	    ,cols: [[ //表头
	       {type:'checkbox', fixed: 'left'}
	      ,{field: 'itemName', title: '药品名称 ', sort: 'true', align: 'center', width:180, fixed: 'left'}
	      ,{field: 'itemType', title: '药品类型', sort: 'true', align: 'center', width:120}
	      ,{field: 'repNumber', title: '数量', sort: 'true', align: 'center', width:160}
	      ,{title: '操作', align: 'center', width:160, fixed: 'right', toolbar: '#barDemo'}
	    ]]
	  	,id: 'testReload'
	  });
	  
	  table.on('tool(demo)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
		  var data = obj.data; //获得当前行数据
		  var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
		  if(layEvent == 'ck'){
			  var repId = data.repId;
			  layer.open({
				  title:'库存详情'
				  ,type: 2
				  ,area: ['64%', '84%']
				  ,content: getRootPath()+'pages/tl/InventoryShowList.html?repId='+repId+"&condition=drug"
			  });
		  }
	  });
	  
	 form.on('select(name)', function(data){
		 var name = $(data.elem).attr("id");
		 var itemName = "";
		 var itemType = "";
		 if(name == "drugName"){
			 itemName = data.value;
			 itemType = $("#drugTypeName").val();
		 }else if(name == "drugTypeName"){
			 itemType = data.value;
			 itemName = $("#drugName").val();
		 }
		 table.reload('testReload',{
			 page: {
	  			curr: 1 //重新从第 1 页开始
  			}
  			,where:{
  				itemName:itemName
  				,itemType:itemType
  			}
		 });
	 }); 
	  
	  var $ = layui.$;
	  
	  //查询物品名称和物品类型
	  var url = getRootPath()+"Repertory/findAllItemNameAndItemType.action";
	  var data = {"filId":"1","condition":"drug"};
	  $.ajax({
		  url:url,
		  data:data,
		  success:function(msg){
			  for(var i=0;i<msg.length;i++){
				  $("#drugName").append("<option value="+msg[i].itemName+">"+msg[i].itemName+"</option>");
				  if(msg[i].itemType != ""){
					  $("#dtName").append("<option value="+msg[i].itemType+">"+msg[i].itemType+"</option>");
				  }
			  }
			  form.render(); //渲染
		  },
		  dataType:"JSON"
	  });
	});